#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: trayicon.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Tray Icon for Vazaar

import os

import gtk

from vazaarlib.constants import IMAGES_DIR
from vazaarlib.ui.widgets.about import AboutDialog

class VazaarStatusIcon():
    def __init__(self, gui):
        self.gui = gui
        self.trayicon = gtk.StatusIcon()
        self.popup = {}
        self.menu = gtk.Menu()
        self.install()

    def setup_menu(self):
        # Add new resource
        mnuitem = gtk.ImageMenuItem('Add a new resource...')
        mnuitem.connect('activate', self.gui.actions.add_new_resource)
        icon = self.gui.widget.render_icon(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON)
        mnuitem.set_image(gtk.image_new_from_pixbuf(icon))
        self.menu.append(mnuitem)

        # Start/Stop clipboard monitoring
        #mnuitem = gtk.CheckMenuItem('Start/Stop clipboard monitoring')
        #mnuitem.connect('activate', self.gui.actions.add_new_resource)
        #icon = self.gui.widget.render_icon(gtk.STOCK_COLOR_PICKER, gtk.ICON_SIZE_BUTTON)
        #mnuitem.set_image(gtk.image_new_from_pixbuf(icon))
        #self.menu.append(mnuitem)

        # Find in Vazaar
        #mnuitem = gtk.ImageMenuItem('Find in Vazaar')
        #mnuitem.connect('activate', self.gui.actions.add_new_resource)
        #icon = self.gui.widget.render_icon(gtk.STOCK_FIND, gtk.ICON_SIZE_BUTTON)
        #mnuitem.set_image(gtk.image_new_from_pixbuf(icon))
        #self.menu.append(mnuitem)

        # Show application window
        mnuitem = gtk.ImageMenuItem('Show Vazaar')
        mnuitem.connect('activate', self.gui.actions.show_application)
        icon = self.gui.widget.render_icon(gtk.STOCK_JUMP_TO, gtk.ICON_SIZE_BUTTON)
        mnuitem.set_image(gtk.image_new_from_pixbuf(icon))
        self.menu.append(mnuitem)

        # About Vazaar
        mnuitem = gtk.ImageMenuItem('About Vazaar')
        mnuitem.connect('activate', self.gui.actions.show_about_dialog)
        icon = self.gui.widget.render_icon(gtk.STOCK_ABOUT, gtk.ICON_SIZE_BUTTON)
        mnuitem.set_image(gtk.image_new_from_pixbuf(icon))
        self.menu.append(mnuitem)

        # Quit Vazaar
        mnuitem = gtk.ImageMenuItem('Quit Vazaar')
        mnuitem.connect('activate', self.quit)
        icon = self.gui.widget.render_icon(gtk.STOCK_QUIT, gtk.ICON_SIZE_BUTTON)
        mnuitem.set_image(gtk.image_new_from_pixbuf(icon))
        self.menu.append(mnuitem)

    def setup_icon(self):
        self.trayicon.set_from_file(os.path.join(IMAGES_DIR + "/Collection.svg"))
        self.trayicon.set_tooltip("Vazaar Personal Information Manager")
        self.trayicon.connect('activate', self.gui.actions.show_application)
        self.trayicon.connect('popup-menu', self.popup_menu_cb, self.menu)
        self.trayicon.set_visible(True)

    def install(self):
        self.setup_menu()
        self.setup_icon()

    def popup_menu_cb(self, widget, button, time, data = None):
        if button == 3:
            if data:
                data.show_all()
                data.popup(None, None, None, 3, time)
        pass


    def quit(self, *args):
        self.gui.actions.stop()




